﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using RG.Common.Configuration;
using RG.Common.WPF.Commands;

namespace RG.Common.WPF.Filters.ToolBarFilters
{


    public class FilterIntervalIntFrameVM : FilterIntervalFrameVM<int>
    {
        public FilterIntervalIntFrameVM(IUnityContainer cfg) : base(cfg)
        {
        }

        protected override int GetDefaultValue()
        {
            return 0;
        }

        protected override bool EqualValues(int a, int b)
        {
            return a == b;
        }
    }

    public class FilterIntervalDecFrameVM : FilterIntervalFrameVM<decimal>
    {
        public FilterIntervalDecFrameVM(IUnityContainer cfg)
            : base(cfg)
        {
        }

        protected override decimal GetDefaultValue()
        {
            return 0;
        }

        protected override bool EqualValues(decimal a, decimal b)
        {
            return a == b;
        }
    }

    [SupportDataBinding]
    public abstract class FilterIntervalFrameVM<T> : FilterBaseParamVM<Interval<T>>
    {
        public ICommand ClearFromValueCmd { get; set; }
        public ICommand ClearToValueCmd { get; set; }


        public FilterIntervalFrameVM(IUnityContainer cfg)
            : base(cfg)
        {
            ClearFromValueCmd = new DelegateCommand(DoClearFromValue);
            ClearToValueCmd = new DelegateCommand(DoClearToValue);
        }

        private void DoClearFromValue()
        {
            FromValue = GetDefaultValue();
        }

        private void DoClearToValue()
        {
            ToValue = GetDefaultValue();
        }

        protected abstract T GetDefaultValue();

        protected abstract bool EqualValues(T a, T b);

        protected override Interval<T> ReturnDefaultEmptyValue()
        {
            return null;
        }


        public T FromValue
        {
            get 
            {
                if (Value == null)
                    return GetDefaultValue();
                return Value.From;
            }
            set
            {
                 RecalcValue(value,ToValue);
            }
        }

        public T ToValue
        {
            get
            {
                if (Value == null)
                    return GetDefaultValue();
                return Value.To;
            }
            set
            {
                RecalcValue(FromValue, value);
            }
        }

        public void RecalcValue(T from, T to)
        {
            if (EqualValues(from, GetDefaultValue()) && EqualValues(to, GetDefaultValue()))
                Value = null;
            else
                Value = new Interval<T>()
                            {
                                From = from,
                                To = to
                            };
        }


        public string FormattedValue
        {
            get
            {
                return !ValueEmpty() ? string.Format("от {0} до {1}", FromValue, ToValue) : EmptyText;
            }
        }

        public override void ClearValueQuiet()
        {
            Value = null;
            FromValue = GetDefaultValue();
            ToValue = GetDefaultValue();
        }

        public override bool ValueEmpty()
        {
            return value == null;
        }
    }
}
